package com.shen.accountbook.greendao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.shen.accountbook.bean.temp.ThirdSortBean;

import com.shen.accountbook.bean.temp.NoteBeanTemp;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "NOTE_BEAN_TEMP".
*/
public class NoteBeanTempDao extends AbstractDao<NoteBeanTemp, Long> {

    public static final String TABLENAME = "NOTE_BEAN_TEMP";

    /**
     * Properties of entity NoteBeanTemp.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property First_sort_id = new Property(1, Long.class, "first_sort_id", false, "FIRST_SORT_ID");
        public final static Property Second_sort_id = new Property(2, Long.class, "second_sort_id", false, "SECOND_SORT_ID");
        public final static Property Third_sort_id = new Property(3, Long.class, "third_sort_id", false, "THIRD_SORT_ID");
        public final static Property Money = new Property(4, float.class, "money", false, "MONEY");
        public final static Property Remarks = new Property(5, String.class, "remarks", false, "REMARKS");
        public final static Property Img = new Property(6, String.class, "img", false, "IMG");
        public final static Property CreateDate = new Property(7, String.class, "createDate", false, "CREATE_DATE");
    }

    private DaoSession daoSession;


    public NoteBeanTempDao(DaoConfig config) {
        super(config);
    }
    
    public NoteBeanTempDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"NOTE_BEAN_TEMP\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"FIRST_SORT_ID\" INTEGER NOT NULL ," + // 1: first_sort_id
                "\"SECOND_SORT_ID\" INTEGER NOT NULL ," + // 2: second_sort_id
                "\"THIRD_SORT_ID\" INTEGER NOT NULL ," + // 3: third_sort_id
                "\"MONEY\" REAL NOT NULL ," + // 4: money
                "\"REMARKS\" TEXT," + // 5: remarks
                "\"IMG\" TEXT," + // 6: img
                "\"CREATE_DATE\" TEXT NOT NULL );"); // 7: createDate
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"NOTE_BEAN_TEMP\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, NoteBeanTemp entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getFirst_sort_id());
        stmt.bindLong(3, entity.getSecond_sort_id());
        stmt.bindLong(4, entity.getThird_sort_id());
        stmt.bindDouble(5, entity.getMoney());
 
        String remarks = entity.getRemarks();
        if (remarks != null) {
            stmt.bindString(6, remarks);
        }
 
        String img = entity.getImg();
        if (img != null) {
            stmt.bindString(7, img);
        }
        stmt.bindString(8, entity.getCreateDate());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, NoteBeanTemp entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getFirst_sort_id());
        stmt.bindLong(3, entity.getSecond_sort_id());
        stmt.bindLong(4, entity.getThird_sort_id());
        stmt.bindDouble(5, entity.getMoney());
 
        String remarks = entity.getRemarks();
        if (remarks != null) {
            stmt.bindString(6, remarks);
        }
 
        String img = entity.getImg();
        if (img != null) {
            stmt.bindString(7, img);
        }
        stmt.bindString(8, entity.getCreateDate());
    }

    @Override
    protected final void attachEntity(NoteBeanTemp entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public NoteBeanTemp readEntity(Cursor cursor, int offset) {
        NoteBeanTemp entity = new NoteBeanTemp( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getLong(offset + 1), // first_sort_id
            cursor.getLong(offset + 2), // second_sort_id
            cursor.getLong(offset + 3), // third_sort_id
            cursor.getFloat(offset + 4), // money
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // remarks
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // img
            cursor.getString(offset + 7) // createDate
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, NoteBeanTemp entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setFirst_sort_id(cursor.getLong(offset + 1));
        entity.setSecond_sort_id(cursor.getLong(offset + 2));
        entity.setThird_sort_id(cursor.getLong(offset + 3));
        entity.setMoney(cursor.getFloat(offset + 4));
        entity.setRemarks(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setImg(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setCreateDate(cursor.getString(offset + 7));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(NoteBeanTemp entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(NoteBeanTemp entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(NoteBeanTemp entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getThirdSortBeanDao().getAllColumns());
            builder.append(" FROM NOTE_BEAN_TEMP T");
            builder.append(" LEFT JOIN THIRD_SORT_BEAN T0 ON T.\"THIRD_SORT_ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected NoteBeanTemp loadCurrentDeep(Cursor cursor, boolean lock) {
        NoteBeanTemp entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        ThirdSortBean thirdSortBean = loadCurrentOther(daoSession.getThirdSortBeanDao(), cursor, offset);
         if(thirdSortBean != null) {
            entity.setThirdSortBean(thirdSortBean);
        }

        return entity;    
    }

    public NoteBeanTemp loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<NoteBeanTemp> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<NoteBeanTemp> list = new ArrayList<NoteBeanTemp>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<NoteBeanTemp> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<NoteBeanTemp> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
